package com.ruoyi.blog.controller.api;

import com.ruoyi.blog.domain.BlogArticle;
import com.ruoyi.blog.domain.BlogTags;
import com.ruoyi.blog.service.IBlogArticleService;
import com.ruoyi.blog.service.IBlogTagsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/blog/tags")
public class TagsController extends BaseController {

    @Autowired
    private IBlogTagsService iBlogTagsService;

    @Autowired
    private IBlogArticleService iBlogArticleService;

    @GetMapping("getTagsList")
    public AjaxResult getTagsList() {
        List<BlogTags> blogTagsList = iBlogTagsService.selectBlogTagsList();
        return AjaxResult.success(blogTagsList);
    }

    @GetMapping("getArticleListByTagName/{tagName}")
    public AjaxResult getArticleListByTagName(@PathVariable String tagName) {
        HashMap<String, Object> result = new HashMap<>();
        BlogArticle bean = new BlogArticle();
        bean.setTags(tagName);

        List<BlogArticle> list = iBlogArticleService.selectBlogArticleList(bean);
        BlogTags info = iBlogTagsService.selectBlogTagsByName(tagName);

        result.put("list", list);
        result.put("info", info);
        return AjaxResult.success(result);
    }
}
